//
//  XYTRegisterViewController.m
//  XYTPayOrderSystem
//
//  Created by wupeng on 2017/8/28.
//  Copyright © 2017年 wupeng. All rights reserved.
//

#import "XYTRegisterViewController.h"
#import "XYTImageTextFieldView.h"
#import "XYTVerifyCodeView.h"
#import "XYTUploadInfoViewController.h"

@interface XYTRegisterViewController ()

@property (nonatomic, strong) XYTImageTextFieldView *phoneView;

@property (nonatomic, strong) XYTImageTextFieldView *passwordView;

@property (nonatomic, strong) XYTImageTextFieldView *rPasswordView;

@property (nonatomic, strong) UITextField *verifyCodeField;

@property (nonatomic, strong) XYTVerifyCodeView *codeBtn;

@end

@implementation XYTRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"用户注册";
    
    [self setupUI];
}

- (void)setupUI{
    
    XYTImageTextFieldView *phoneView = [[XYTImageTextFieldView alloc] initWithFrame:CGRectMake(30, 150, kScreen_Width-30*2, 40)];
    phoneView.imgView.image = [UIImage imageNamed:@"icon_login_phone"];
    phoneView.textField.placeholder = @"请输入手机号";
    [self.view addSubview:phoneView];
    self.phoneView = phoneView;
    
    XYTImageTextFieldView *passwordView = [[XYTImageTextFieldView alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(phoneView.frame)+10, kScreen_Width-30*2, 40)];
    passwordView.imgView.image = [UIImage imageNamed:@"icon_login_password"];
    passwordView.textField.placeholder = @"请输入密码";
    passwordView.textField.secureTextEntry = YES;
    [self.view addSubview:passwordView];
    self.passwordView = passwordView;
    
    XYTImageTextFieldView *rPasswordView = [[XYTImageTextFieldView alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(passwordView.frame)+10, kScreen_Width-30*2, 40)];
    rPasswordView.imgView.image = [UIImage imageNamed:@"icon_login_password"];
    rPasswordView.textField.placeholder = @"请再次输入密码";
    rPasswordView.textField.secureTextEntry = YES;
    [self.view addSubview:rPasswordView];
    self.rPasswordView = rPasswordView;
    
    UITextField *verifyCodeField = [[UITextField alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(rPasswordView.frame) + 10, (kScreen_Width - 30*2-8)*0.5, 40)];
    verifyCodeField.borderStyle = UITextBorderStyleNone;
    [verifyCodeField.layer setBorderWidth:1.0];
    [verifyCodeField.layer setCornerRadius:2.0];
    [verifyCodeField.layer setBorderColor:[UIColor lightGrayColor].CGColor];
    verifyCodeField.font = [UIFont systemFontOfSize:14.0f];
    verifyCodeField.textAlignment = NSTextAlignmentLeft;
    verifyCodeField.keyboardType = UIKeyboardTypeNumberPad;
    verifyCodeField.clearButtonMode = UITextFieldViewModeWhileEditing;
    //设置居中显示Placeholder
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.alignment = NSTextAlignmentCenter;
    NSAttributedString *attri = [[NSAttributedString alloc] initWithString:@"请输入验证码" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0], NSParagraphStyleAttributeName:style}];
    verifyCodeField.attributedPlaceholder = attri;
    [self.view addSubview:verifyCodeField];
    self.verifyCodeField = verifyCodeField;
    
    XYTVerifyCodeView *codeBtn = [[XYTVerifyCodeView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(verifyCodeField.frame)+8, CGRectGetMaxY(rPasswordView.frame) + 10, (kScreen_Width - 30*2-8)*0.5, 40)];
    [codeBtn addTarget:self action:@selector(getVerifyCode) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:codeBtn];
    self.codeBtn = codeBtn;
    
    UIButton *registerBtn = [[UIButton alloc] initWithFrame:CGRectMake((kScreen_Width-150)*0.5, CGRectGetMaxY(codeBtn.frame)+10, 150, 40)];
    [registerBtn.layer setCornerRadius:2.0];
    registerBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [registerBtn setBackgroundColor:[UIColor redColor]];
    [registerBtn setTitle:@"立即注册" forState:UIControlStateNormal];
    [registerBtn addTarget:self action:@selector(sendRegister) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registerBtn];
}

//发送验证码
- (void)getVerifyCode{
    [UIView showHUD:@"验证码发送中..."];
    NSString *phone = self.phoneView.textField.text;
    NSString *signStr = [NSString stringWithFormat:@"%@%@%@",AppKey,AppSecret,phone];
    NSString *md5Sign = [Md5Util md5:signStr];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params xyt_setObj:phone forKey:@"phone"];
    [params xyt_setObj:md5Sign forKey:@"sign"];
    XYTWeakSelf(self)
    [XYTNetRequest POST:GetVCodeUrl parameters:params isNeedKey:NO success:^(id responseObject) {
        XYTStrongSelf(weakSelf)
        [UIView hideHUD];
        [UIView showCustomToast:@"验证码发送成功"];
        [strongSelf.codeBtn startCountdown];//开始计数
    } failure:^(NSString *errorMsg) {
        XYTStrongSelf(weakSelf)
        [UIView hideHUD];
        [strongSelf showAlertWithTitle:errorMsg];
    }];
    
}
//开始注册
- (void)sendRegister{
    [UIView showHUD:nil];
    NSString *phone = self.phoneView.textField.text;
    NSString *password = self.passwordView.textField.text;
    NSString *cPassword = self.rPasswordView.textField.text;
    NSString *smsCode = self.verifyCodeField.text;
    NSString *signStr = [NSString stringWithFormat:@"%@%@%@%@%@%@",AppKey,AppSecret,phone,password,cPassword,smsCode];
    NSString *md5Sign = [Md5Util md5:signStr];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params xyt_setObj:phone forKey:@"phone"];
    [params xyt_setObj:password forKey:@"password"];
    [params xyt_setObj:cPassword forKey:@"confirmPassword"];
    [params xyt_setObj:smsCode forKey:@"smsCode"];
    [params xyt_setObj:md5Sign forKey:@"sign"];
    XYTWeakSelf(self);
    [XYTNetRequest POST:RegisterUrl parameters:params isNeedKey:YES success:^(id responseObject) {
        XYTStrongSelf(weakSelf)
        [UIView hideHUD];
        [UIView showCustomToast:@"注册成功"];
        //更新用户信息，并保存
        NSDictionary *responseDic = (NSDictionary *)responseObject;
        long userId = [responseDic[@"userId"] longValue];
        [XYTUserInfoManager sharedManager].userId = userId;
        [[XYTUserInfoManager sharedManager] saveUserInfo];
        //注册完成
        NSMutableArray *list = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
        [list removeObject:strongSelf];
        //注册完成跳上传用户信息界面，暂时不跳，返回上级登录页面
//        XYTUploadInfoViewController *uploadVC = [[XYTUploadInfoViewController alloc] init];
//        [list insertObject:uploadVC atIndex:list.count];
        strongSelf.navigationController.viewControllers = [NSArray arrayWithArray:list];
    } failure:^(NSString *errorMsg) {
        XYTStrongSelf(weakSelf)
        [UIView hideHUD];
        [strongSelf showAlertWithTitle:errorMsg];
        
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)dealloc{
    [self.codeBtn removeTimer];
    self.codeBtn = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
